//
//  HMRemindCell.swift
//  HealthMonitoring
//
//  Created by 珠海正杰 on 2018/8/14.
//  Copyright © 2018年 YTYanK. All rights reserved.
//

import UIKit
import SwiftyJSON

/// 头部view
class RemindHeadView: UIView {
       let colorView = UIView()
       let title = UILabel()
       let time = UILabel()
       var json: JSON?
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.darkGray
        setupViews(view: self)

    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
   private func setupViews(view: UIView) {
        view.addSubview(colorView)
        view.addSubview(title)
        view.addSubview(time)
        
        colorView.centerYAlign(view: view)
        colorView.leadingAlign(view: view, predicate: "10")
        colorView.heightConstrain(predicate: "10")
        colorView.widthConstrain(predicate: "10")
        colorView.layer.cornerRadius = 10/2
        colorView.layer.masksToBounds = true
        
        // 10
        
        title.centerYAlign(view: view)
        title.leadingHorizontally(view: colorView, predicate: "10")
        title.trailingAlign(view: time)
        title.text = "---"
        
        time.centerYAlign(view: view)
        time.trailingAlign(view: view, predicate: "-10")
        time.text = "---"
        
    }
    
    func setRemindHeadJson(json: JSON) {
        let r_color = json["color"].stringValue
        if  r_color  ==  "red" {
          self.colorView.backgroundColor = .red
        }else if r_color == "blue" {
          self.colorView.backgroundColor = .blue
        }else  {
          self.colorView.backgroundColor = UIColor.yellow
        }
        
        self.title.text = json["title"].stringValue
        self.time.text = json["time"].stringValue
    }
}

/// 左 状态view
class RemindLeftItme: UIView {
    let icon = UIImageView()
    let state = UILabel()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupViews(view: self)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupViews(view: UIView)  {
        view.addSubview(icon)
        view.addSubview(state)
        
        icon.centerXAlign(view: view)
        icon.centerYAlign(view: view, predicate: "-10")
        icon.image = UIImage.init(named: "device_.png")
        icon.widthConstrain(predicate: "30")
        icon.heightConstrain(predicate: "30")
        
        state.topVertically(view: icon, predicate: "10")
        state.textAlignment = .center
        state.leadingAlign(view: view, predicate: "0")
        state.trailingAlign(view: view, predicate: "0")
    }
    
    func setJson(json: JSON) {
        icon.image = UIImage.init(named: "device_.png")
        state.text = "未知状态"
    }
}



class HMRemindCell: UITableViewCell {
    
   var cellHeight: CGFloat = 0.0
//   let itme = HMRemindItme()
   let headView = RemindHeadView()
   let leftItme = RemindLeftItme()
   var dataList: Array<Any>? = nil
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }
    
    
    class func cell(tableView: UITableView, cellForRowAt indexPath: IndexPath) -> HMRemindCell? {
        let identifier = "HMRemindCell"  //NSStringFromClass(self)
        var cell:HMRemindCell? =  tableView.dequeueReusableCell(withIdentifier: identifier) as? HMRemindCell
        
        if cell == nil {
            cell = HMRemindCell.init(style: .default, reuseIdentifier: identifier)
        }
//        cell?.backgroundColor = .blue
        cell?.setupViews(tableView: tableView, view: (cell?.contentView)!)
        cell?.setJson(json: ["title": "标题\(indexPath.row)", "time": "12:00"])
        return cell
//
    }

    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        //fatalError("init(coder:) has not been implemented")
    }
    
    func setupViews(tableView: UITableView, view: UIView) {
           view.addSubview(headView)
           view.addSubview(leftItme)
        
            headView.topAlign(view: view, predicate: "0")
            headView.leadingAlign(view: view, predicate: "0")
            headView.trailingAlign(view: view, predicate: "0")
            headView.heightConstrain(predicate: "44")
            headView.addLayer(isAddLayer: (false, false, false, true), color: .gray, crude: 2, layout: (w: tableView.wWidth, h: 44))
        
           leftItme.topVertically(view: headView, predicate: "0")
           leftItme.leadingAlign(view: view, predicate: "0")
           leftItme.bottomAlign(view: view, predicate: "0")
           leftItme.width(view: view, predicate: "*0.2")
           leftItme.setJson(json: nil)
       
        let son:JSON = ["A": ["板蓝根 一天一次","板蓝根2 一天四分之一", "板蓝根3 一天四至五分"], "B":  ["板蓝根1 一天一次","板蓝根3 一天四分之一", "板蓝根4 一天四至五分"], "state": "已确认", "icon": "device_.png"]
        let itme = HMRemindItme.init(setItmeJson: son, frame: CGRect.init(x: 0, y: 0, width: 100, height: 0))
           view.addSubview(itme)

            let itemH = itme.remindItmeHeight
            itme.topVertically(view: headView, predicate: "0")
            itme.leadingHorizontally(view: leftItme, predicate: "0")
            itme.trailingAlign(view: view, predicate: "0")
            itme.heightConstrain(predicate: "\(itemH)")
            self.cellHeight = itemH + 44.0
        }
    
    func setJson(json: JSON) {
        headView.setRemindHeadJson(json: ["color":"red", "title": json["title"].stringValue, "time": json["time"].stringValue])
     
    }
}
